package com.sumainfor.wx.tenant.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.wx.tenant.service.HousingFindService;
import com.sumainfor.wx.tenant.service.TctenTenantLikeService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author Jack Chen
 * @Create 2019-02-13 14:22
 */
@RestController
@RequestMapping("/housing")
@Log4j2
public class HousingFindController {

    @Autowired
    private HousingFindService hfs;

    @Autowired
    private TctenTenantLikeService tctenTenantLikeService;

    /**
     * 查询房源
     *
     * @return
     */
    @RequestMapping("/findHousingList")
    public JsonResult findHousingList(@RequestParam Map<String, Object> params) {
        log.info("Params ===> {}", params);
        return hfs.findHousing(params);
    }

    /**
     * 获得热门搜索排行数据
     *
     * @param size
     * @return
     */
    @RequestMapping("/getSearchHot")
    public JsonResult getSearchHot(@RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return JsonResult.success(hfs.getHotList(size));
    }

    /**
     * 获得历史搜索记录
     *
     * @return
     */
    @RequestMapping("/getHistorySearchList")
    public JsonResult getHistorySearchList() {
        List<String> search = hfs.getHistorySearch();
        return JsonResult.success(search);
    }

    /**
     * 清空历史搜索记录
     *
     * @return
     */
    @PostMapping("/clearHistorySearchData")
    public JsonResult clearHistorySearchData() {
        boolean state = hfs.clearHistorySearchData();
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    /**
     * 根据编号查询房源的详细信息
     *
     * @param infoId
     * @return
     */
    @RequestMapping("/getHousingInfoById")
    public JsonResult getHousingInfoById(String infoId) {
        if (StringUtils.isBlank(infoId)) {
            return JsonResult.errorForEmpty();
        }
        return hfs.getHousingInfoById(infoId);
    }

    /**
     * 添加或者移除我的喜爱房源
     *
     * @param infoId
     * @return
     */
    @PostMapping("/actionLikeHousing")
    public JsonResult addLikeHousing(String infoId) {
        if (StringUtils.isBlank(infoId)) {
            return JsonResult.errorForEmpty();
        }
        return hfs.actionLikeHousing(infoId);
    }

    /**
     * 预约房源
     *
     * @param params infoId 房源详表编号
     *               date 预约时间 long类型
     *               remark 备注
     *               shelfId 销售编号
     *               MakeUserName 约看人姓名
     * @return
     */
    @PostMapping("/makeHousing")
    public JsonResult makeHousing(@RequestBody Map<String, Object> params) {
        String[] keys = {"infoId", "shelfId", "MakeUserName"};

        if (!FieldCheckUtil.isMapFieldNotNull(params, keys)) {
            return JsonResult.errorForEmpty();
        }
        return hfs.makeHousing(params);
    }

    /**
     * 房源预约列表数据
     *
     * @return
     */
    @PostMapping("/getMakeHousingList")
    public JsonResult getMakeHousingList() {
        return hfs.getMakeHousingList();
    }

    /**
     * 评价一个预约单
     *
     * @param params
     * @return
     */
    @PostMapping("/commentMakeOrder")
    public JsonResult commentMakeOrder(@RequestBody Map<String, Object> params) {
        return hfs.commentMakeOrder(params);
    }

    /**
     * 根据房源详表编号和租客的编号查询房源是否是喜爱房源
     */
    @GetMapping("/selectLikeHousing")
    public JsonResult selectLikeHousing(@RequestParam Map<String, Object> params) {
        return tctenTenantLikeService.selectLikeHousing(params);
    }
}
